<template>
  <div>
    <el-dialog :title="form._id ? '修改' :'新增'" :visible.sync="dialogFormVisible" width="400px">
      <el-form
        :model="form"
        status-icon
        :rules="rules"
        ref="form"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="图片" prop="img">
          <upload
          action="/upload"
          :cicle="true"
          :width="300"
          :height="300"
          :on-success="onSuccess">
            
        <div @click="openFile" style="overflow:hidden; width:100px;height:100px;font-size:32px;border:1px dashed #ccc; text-align:center;line-height:100px">
         <img style="max-width:100%;max-height:100%;" v-if="form.img" :src="form.img"/>
         <span v-else>+</span>
         </div>
         <input ref="file" style="display:none" multiple accept="image/*" type="file" @change="changeFile"/>
          </upload>
        </el-form-item>

        <el-form-item label="标题" prop="title">
          <el-input
            type="text"
            v-model="form.title"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="跳转地址" prop="url">
          <el-input
            type="text"
            v-model="form.url"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择">
            <el-option
              v-for="type in types"
              :key="type.value"
              :label="type.label"
              :value="type.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <!-- <el-button type="primary" @click="submit">确定</el-button> -->
          <el-button type="primary" @click="submit">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import api from '@/api'
import Upload from '../../ElementUI/Form/Upload.vue';
export default {
  name: "shouyecategoryEdit",
  components: {Upload},
  data() {
    return {
    
      dialogFormVisible: false,
      form: {
        img: "",
        type: "category",
        url: "",
        title: "",
        _id:0
      },
      rules: {
        img: [{ required: true, message: "图片不能为空", trigger: "blur" }],
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
      },
      types: [
        { label: "轮播图", value: "banner" },
        { label: "分类", value: "category" },
        { label: "推荐", value: "recommend" },
      ]
    };
  },
  methods: {
    //新增
    add() {
      //初始化
      this.form={
        img: "",
        type: "banner",
        url: "",
        title: "",
        _id:0
      }
      this.dialogFormVisible = true;
    },
    //修改数据
    edit(row){
      this.form=row
 this.dialogFormVisible = true;
    } ,

    changeFile(e){
      let files=e.target.files
      let formDate= new FormData()
      formDate.append('file',files[0])

      this.$http({
        url:'/upload',
        method:'POST',
        data:formDate
      }).then(res =>{
        var path=res.path.split('\\')
        path = path[0] +'/' +path[1]
        console.log(path);
        if(res.path) this.form.img= api.baseURL +'/' + path
console.log(res);
      })

    }, 
    openFile(){
      let fileElement=this.$refs.file
      fileElement.click()
    },
   onSuccess(res){
     //拼装图片路径赋值给表单
     console.log(res);
     this.form.img =api.baseURL + '/' +res.path
   },
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.indexId=this.form._id
          this.$http({
            url: "/admin/index",
            method:this.form._id ? 'PUT': "POST",//通过indexId判断是新增还是修改
            data: this.form,
          }).then((res) => {
            if (!res.errorMsg) {
              this.dialogFormVisible = false;
              this.$emit('load',this.form.indexId ? undefined : 1)
            }
          });
        }
      });
    },
  },

  created() {
  },
  mounted() {},
};
</script>

<style>
.pagination{
  margin-top: 2%;
  margin-left: 3%;
}
</style>
